package com.enjoy.studyfragment.fragment;

import android.widget.ExpandableListView;

import com.enjoy.mjbaseframe.okgo.JsonCallback;
import com.enjoy.mjbaseframe.okgo.OkGoLoader;
import com.enjoy.mjbaseframe.util.GsonUtil;
import com.enjoy.mjbaseframe.util.SPUtil;
import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.bean.ClassList;
import com.enjoy.studyfragment.bean.ContactSelectBean;
import com.enjoy.studyfragment.constant.Constants;
import com.enjoy.studyfragment.widget.FloatingGroupExpandableListView;
import com.enjoy.studyfragment.widget.WrapperExpandableListAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/3/16.
 */

public class QQGroupFragment extends TopBarFragment {
    @BindView(R.id.explistview)
    FloatingGroupExpandableListView explistview;

    private PinnedHeaderExpandableAdapter adapter;
    private List<ContactSelectBean> list = new ArrayList<>();

    @Override
    public Object setLayout() {
        return R.layout.fragment_qq_group;
    }

    @Override
    public void init() {
        super.init();
        initExpand();
        post();
    }
    public void initExpand(){
        adapter = new PinnedHeaderExpandableAdapter(getContext(),explistview);
        WrapperExpandableListAdapter wrapAdapter = new WrapperExpandableListAdapter(adapter);
        explistview.setAdapter(wrapAdapter);
        explistview.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                int count = explistview.getExpandableListAdapter().getGroupCount();
                for(int j = 0; j < count; j++){
                    if(j != groupPosition){
                        explistview.collapseGroup(j);
                    }
                }
            }
        });
    }

    public void post(){
        ClassList classList= (ClassList) SPUtil.getObjectFromSP(Constants.CLASSLIST);
        Map map = new WeakHashMap();
        map.put("teacher_id", classList.getUser_id()+"");//用户ID == 老师ID
        OkGoLoader.getInstance().post(Constants.PUSHANDCHOOSE, map, new JsonCallback() {
            @Override
            public void onComplete(String result, String totalPageNum) {
                list = GsonUtil.getInstance().getList(result,ContactSelectBean[].class);
                adapter.setData(list);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int status, String msg) {
                super.onFailure(status, msg);
            }
        });
    }
}
